package com.fenghongzhang.music.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenghongzhang.music.R;
import com.fenghongzhang.music.entity.Music;
import com.fenghongzhang.music.util.MusicUtils;

import java.util.List;

public class MusicAdapter extends BaseAdapter {


    private List<Music> list;
    private Context context;

    public MusicAdapter(List<Music> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new ViewHolder();

            convertView = LayoutInflater.from(context).inflate(R.layout.music_list_layout, null);

            viewHolder.title = convertView.findViewById(R.id.item_mymusic_song);
            viewHolder.artist = convertView.findViewById(R.id.item_mymusic_singer);
            viewHolder.duration = convertView.findViewById(R.id.item_mymusic_duration);
            viewHolder.img = convertView.findViewById(R.id.item_img);

            convertView.setTag(viewHolder);

        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(list.get(position).getTitle());


        viewHolder.img.setImageBitmap(BitmapFactory.decodeFile(list.get(position).getImgPath()));

        //格式化时间
        String duration = list.get(position).getDuration();
        viewHolder.duration.setText(MusicUtils.formatTime(Integer.parseInt(duration)));
        viewHolder.artist.setText(list.get(position).getArtist());

        return convertView;
    }


    class ViewHolder{
        TextView title;
        TextView artist;
        TextView duration;
        ImageView img;

    }


}
